package edu.gatech.cc.concolic.constraint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;


public abstract class NaryConstraint implements Constraint {

	protected final Collection<Constraint> constraints;

	public NaryConstraint(Collection<Constraint> constraints) {
		this.constraints = new ArrayList<Constraint>(constraints);
	}

	public NaryConstraint(Constraint... constraints) {
		this(Arrays.asList(constraints));
	}
	
	public Collection<Constraint> constraints() {
		return constraints;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("(");
		builder.append(getClass().getSimpleName());
		for (Constraint e : constraints) {
			builder.append(" ");
			builder.append(e);
		}
		builder.append(")");
		return builder.toString();
	}
	
	public int hashCode() {
		int result = 9;
		for(Constraint e : constraints) {
			result = 37 * result + e.hashCode();
		}
		return result;
	}
	
	public boolean equals(Object o) {
		if(o instanceof NaryConstraint) {
			NaryConstraint other = (NaryConstraint) o;
			if(other.getClass().equals(getClass()) && other.constraints.equals(constraints)) {
				return true;
			}
		}
		return false;
	}
}
